package com.uetoken.cn.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.uetoken.cn.R;
import com.uetoken.cn.adapter.BankListAdapter;
import com.uetoken.cn.base.BaseActivity;
import com.uetoken.cn.bean.BankListBean;
import com.uetoken.cn.common.ApiHelper;
import com.uetoken.cn.network.OkHttpUtils;
import com.uetoken.cn.network.Params;
import com.uetoken.cn.network.WebResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class BankCardActivity extends BaseActivity implements WebResponse {

    @BindView(R.id.iv_activity_bank_card_back)
    ImageView    mIvBack;
    @BindView(R.id.tv_activity_bank_card_title)
    TextView     mTvTitle;
    @BindView(R.id.ry_activity_bank)
    RecyclerView mRyBank;
    @BindView(R.id.tv_activity_bank_new_add)
    TextView     mTvNewAdd;
    @BindView(R.id.rl_activity_bank_new_no)
    RelativeLayout mRlNo;
    private List<BankListBean.DataBean> mList = new ArrayList<>();
    private BankListAdapter mAdapter;
    /**
     * 获取银行卡的网络请求吗
     */
    private static final int NETWORK_REQUEST_BANK = 10;
    /**
     * 删除银行卡的网络请求码
     */
    private static final int NETWORK_REQUEST_DELETE_BANK = 20;
    /**
     * 跳转到新增银行卡界面的intent请求码
     */
    private static final int INTENT_REQUEST_CREATE_BANK_CARD = 20;



    @Override
    public int getLayoutId() {
        return R.layout.activity_bank;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        /*状态栏沉浸式*/
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this,android.R.color.white),true);
        BarUtils.setStatusBarLightMode(this,true);

        mTvTitle.setText(getResources().getString(R.string.tv_bank_card));

        mRyBank.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
    }

    @Override
    public void initData() {
        mAdapter = new BankListAdapter(R.layout.item_bind_bank_list,mList);
        mRyBank.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {
                new QMUIDialog.MessageDialogBuilder(BankCardActivity.this).setTitle(getResources().getString(R.string.str_delete_bank_card)).setMessage(getResources().getString(R.string.str_will_unable_to_recover))
                        .addAction(getResources().getString(R.string.str_cancel), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                }).addAction(getResources().getString(R.string.str_confirm), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        //删除当前选择的银行卡
                        OkHttpUtils.getInstance().getStringWithParam(BankCardActivity.this,ApiHelper.getDeleteBankUrl(),NETWORK_REQUEST_DELETE_BANK,
                                Params.getDeleteBankParams(String.valueOf(mList.get(position).getBankaccoutid())));
                        mAdapter.remove(position);
                        if (mList.size()==0){
                            mRlNo.setVisibility(View.VISIBLE);
                        }
                    }
                }).show();
            }
        });

        getBankList();
    }

    @Override
    public void init() {

    }

    /**
     * 获取银行卡列表
     */
    private void getBankList() {
        if (mList.size()!=0){
            mList.clear();
        }
        showLoading();
        OkHttpUtils.getInstance().getStringWithParam(this, ApiHelper.getUserBindBankUrl(), NETWORK_REQUEST_BANK,Params.getBindBankParams());
    }

    @OnClick({R.id.iv_activity_bank_card_back, R.id.tv_activity_bank_new_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_activity_bank_card_back:
                finish();
                break;
            case R.id.tv_activity_bank_new_add:
                Intent intent = new Intent(this,AddBankCardActivity.class);
                startActivityForResult(intent,INTENT_REQUEST_CREATE_BANK_CARD);
                break;
        }
    }

    @Override
    public void onSuccessResponse(Call call, String response, int requestCode) throws IOException {
        dissmissDialog();
        switch (requestCode){
            case NETWORK_REQUEST_BANK:
                BankListBean bankListBean = new Gson().fromJson(response,BankListBean.class);
                if (bankListBean.getResult()>0){
                    if (bankListBean.getData().size()>0){
                        mRlNo.setVisibility(View.GONE);

                        mList.addAll(bankListBean.getData());
                        mAdapter.notifyDataSetChanged();
                    }else{
                       mRlNo.setVisibility(View.VISIBLE);
                    }
                }else{
                    ToastUtils.showShort(bankListBean.getMessage());
                }
                break;
            case NETWORK_REQUEST_DELETE_BANK:
                JsonObject obj = new JsonParser().parse(response).getAsJsonObject();
                String message = obj.get("message").getAsString();
                int result = obj.get("result").getAsInt();
                if (result>0){

                }else{
                    ToastUtils.showShort(message);
                }
                break;
        }
    }

    @Override
    public void onFailResponse(Call call, IOException e, int requestCode) {
        dissmissDialog();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==INTENT_REQUEST_CREATE_BANK_CARD && resultCode==RESULT_OK){
            //添加银行卡成功后重新刷新下银行卡列表
            getBankList();
        }
    }
}
